import {
    Message
} from 'element-ui';
import Vue from "vue";
import VueRouter from 'vue-router';
import Cookies from 'js-cookie';

import Home from "../pages/home"
import Login from "../pages/login"

/* 让所有的组件无条件使用routr-link route-view
让所有的组件无条件使用$router 和$route */
Vue.use(VueRouter);

const router = new VueRouter({
    routes: [{
            path: "/",
            redirect: "/login"
        },
        {
            path: '/home',
            component: Home,
            name: "Home"
        },
        {
            path: '/login',
            component: Login,
            name: "Login"
        }
    ]
});


router.beforeEach((to, from, next) => {
    // 判断是否已经登录
    if (to.path === '/home' && !Cookies.get("token")) {
        next({
            path: '/login'
        });
    } else next(); //已经登陆 放行
});

// 通过导航栏刷新 路径是不会变的 随意通过路径判断 是否是第一次从登录页面进入home页面
router.afterEach((to, from) => {
    // 确保提示一次 登陆成功
    if (to.path === '/home' && from.path === "/login") {
        Message.success({
            type: "success",
            message: "登陆成功"
        })
    }
})


export default router;